//------------------------------------------------------------------------------
// Copyright (c) 2008 www.dnaide.com
// Licensed under the MIT (MIT-LICENSE.txt)
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace DNAide.Collections
{
	/// <summary>
	/// Stack helper methods
	/// </summary>
	public static partial class StackAide
	{
		/// <summary>
		/// Returns a string Stack from a comma separated string.
		/// Trims spaces, does not allow duplicates and ignores empty entities
		/// </summary>
		/// <param name="value">value</param>
		/// <returns>Stack&lt;String&gt;</returns>
		public static Stack<String> GetFromString(String value)
		{
			return GetFromString(value, new char[] { ',' }, true, false, StringSplitOptions.RemoveEmptyEntries);
		}


		/// <summary>
		/// Returns a string Stack from a comma separated string.
		/// Trims spaces, does not allow duplicates and ignores empty entities
		/// </summary>
		/// <param name="value">value</param>
		/// <param name="delimeter">delimeter</param>
		/// <returns>Stack&lt;String&gt;</returns>
		public static Stack<String> GetFromString(String value, Char[] delimeter)
		{
			return GetFromString(value, delimeter, true, false, StringSplitOptions.RemoveEmptyEntries);
		}


		/// <summary>
		/// Returns a string Stack from a delimetered string.
		/// Does not allow duplicates and ignores empty entities
		/// </summary>
		/// <param name="value">value</param>
		/// <param name="delimeter">delimeter</param>
		/// <param name="trimSpaces">trimSpaces</param>
		/// <returns>Stack&lt;String&gt;</returns>
		public static Stack<String> GetFromString(String value, Char[] delimeter, Boolean trimSpaces)
		{
			return GetFromString(value, delimeter, trimSpaces, false, StringSplitOptions.RemoveEmptyEntries);
		}


		/// <summary>
		/// Returns a string Stack from a delimetered string.
		/// Ignores empty entities
		/// </summary>
		/// <param name="value">value</param>
		/// <param name="delimeter">delimeter</param>
		/// <param name="trimSpaces">trimSpaces</param>
		/// <param name="allowDuplicates">allowDuplicates</param>
		/// <returns>Stack&lt;String&gt;</returns>
		public static Stack<String> GetFromString(String value, Char[] delimeter, Boolean trimSpaces, Boolean allowDuplicates)
		{
			return GetFromString(value, delimeter, trimSpaces, allowDuplicates, StringSplitOptions.RemoveEmptyEntries);
		}


		/// <summary>
		/// Returns a string Stack from a delimetered string.
		/// </summary>
		/// <param name="value">value</param>
		/// <param name="delimeter">delimeter</param>
		/// <param name="trimSpaces">trimSpaces</param>
		/// <param name="allowDuplicates">allowDuplicates</param>
		/// <param name="splitOptions">splitOptions</param>
		/// <returns>Stack&lt;String&gt;</returns>
		public static Stack<String> GetFromString(String value, Char[] delimeter, Boolean trimSpaces, Boolean allowDuplicates, StringSplitOptions splitOptions)
		{
			Stack<String> retval = new Stack<string>();

			if (string.IsNullOrEmpty(value))
				return retval;

			if (delimeter == null)
				throw new ArgumentException("delimeter");


			string[] temp = value.Split(delimeter, splitOptions);
			for (int i = 0; i < temp.Length; i++)
			{
				string valueToAdd = temp[i];
				if (trimSpaces)
					valueToAdd = temp[i].Trim();

				if (!allowDuplicates)
				{
					if (retval.Contains(valueToAdd))
						continue;
				}
				retval.Push(valueToAdd);
			}
			return retval;
		}
	}
}